//
//  YFLlittleViewController.m
//  Music
//
//  Created by dllo on 16/6/7.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "YFLlittleViewController.h"
#import "YFLcommentViewController.h"
#import "MyTools.h"
#import "NianFeilongModel.h"
#import "YFLCollectionViewCell.h"
#import "YFLmvModel.h"
#import "YFLalbumCollectionViewCell.h"
#import "YFLalbumModel.h"
#import "YFLlittleAlbumCollectionViewCell.h"
#import "YFLinformationCollectionViewCell.h"

#import "YFLinformationModel.h"
@interface YFLlittleViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,assign) BOOL selected;
@property(nonatomic,assign) NSString *shoucangCount;
@property(nonatomic,strong) UISegmentedControl *segmentController;

@property(nonatomic,strong) UIButton *shoucangButton;

@property(nonatomic,strong) UILabel *shoucangLabel;

@property(nonatomic,strong) UILabel *commentLabel;

@property(nonatomic,strong) UICollectionView *mainCollectionView;
@property(nonatomic,strong) UIView *topView;

@property(nonatomic,strong) NSMutableArray *sourceArray;

@property(nonatomic,strong) NSMutableArray *mvArray;

@property(nonatomic,strong) NSMutableArray *albumArray;

@property(nonatomic,strong) NSMutableArray *informationArray;

@property(nonatomic,strong) NSDictionary *informationDictionary;

@property(nonatomic,strong) NSDictionary *mvUrlDic;

@property(nonatomic,strong) NSMutableArray *MVstartArray;
@end

@implementation YFLlittleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barStyle = 1;
    
    self.navigationItem.title = self.nameTitle;
    self.view.backgroundColor =[UIColor  redColor];
    //创建collectionView
    [self createColllectionView];
    
    //顶部视图
    //[self creatView];
    
    //创建segMentControl
    [self segmentControl];
    self.segmentController.selectedSegmentIndex = 0;
    //获取数据
    [self getNetData];
//    //获取评论数据
    [self getCommentNetData];
//    //获取MV数据
    [self getMVData];
//    //获取专辑数据
    [self getAlbumNetData];
    //歌手信息
    [self getInformationData];
    
    
    
   
}
//创建segmentControl
-(void)segmentControl
{
self.segmentController =[[UISegmentedControl alloc]initWithItems:@[@"单曲",@"专辑",@"MV",@"歌手信息"]];
    //self.segmentController.selectedSegmentIndex = 0;
    self.segmentController.frame = CGRectMake(0, Screenheight/2-94, Screenwidth, 50);
                             
    NSDictionary *selecteTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:18], NSForegroundColorAttributeName:[UIColor whiteColor]};
    [self.segmentController setTitleTextAttributes:selecteTextAttributes forState:UIControlStateSelected]; // 设置文字属性
    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],
                                               NSForegroundColorAttributeName: [UIColor lightTextColor]};
    [self.segmentController setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    [self.segmentController addTarget:self action:@selector(switchPage:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.segmentController];
    
}
//顶部视图
-(void)creatView
{
    self.topView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, Screenwidth, Screenheight/2-94)];
    self.topView.backgroundColor = [UIColor yellowColor];
    UIImageView *backImageView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Screenwidth, Screenheight/2-94)];
    [backImageView sd_setImageWithURL:[NSURL URLWithString:self.photoString] placeholderImage:[UIImage imageNamed:@"占"]];
    
    backImageView.userInteractionEnabled = YES;
    UIBlurEffect *effect =[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    
    UIVisualEffectView *effictView =[[UIVisualEffectView alloc]initWithEffect:effect];
    effictView.frame = CGRectMake(0, 0, backImageView.frame.size.width, backImageView.frame.size.height);
    [backImageView addSubview:effictView];
    [self.topView addSubview:backImageView];
    
    UIImageView *topImageView = [[UIImageView alloc]initWithFrame:CGRectMake((Screenwidth-90)/2, (self.topView.frame.size.height-90)/2-30, 90, 90)];
   // topImageView.backgroundColor =[UIColor redColor];
    [topImageView sd_setImageWithURL:[NSURL URLWithString:self.photoString] placeholderImage:[UIImage imageNamed:@"占.jpg"]];
    topImageView.layer.cornerRadius = 45;
    topImageView.layer.masksToBounds = YES;
    [backImageView  addSubview:topImageView];
    [self.view addSubview:self.topView];
    
    UILabel *mainLabel =[[UILabel alloc]initWithFrame:CGRectMake(80, self.topView.frame.size.height-90, Screenwidth-160, 50)];
   // mainLabel.backgroundColor =[UIColor greenColor];
    mainLabel.text = self.nameTitle;
    mainLabel.textAlignment = 1;
    [backImageView  addSubview:mainLabel];
    
    UIView *shoucangView =[[UIView alloc]initWithFrame: CGRectMake(30, (self.topView.frame.size.height-90)/2-20, 60, 60)];
    //shoucangView.backgroundColor =[UIColor greenColor];
    [backImageView  addSubview:shoucangView];
    
    self.shoucangLabel =[[UILabel alloc]initWithFrame:CGRectMake(0, 45, 60, 15)];
   // self.shoucangLabel.backgroundColor = [UIColor magentaColor];
    self.shoucangLabel.textAlignment = 1;
    self.shoucangLabel.textColor = [UIColor whiteColor];
    self.shoucangLabel.adjustsFontSizeToFitWidth = YES;
    [shoucangView addSubview:_shoucangLabel];
    
    //收藏按钮
    self.shoucangButton =[UIButton buttonWithType:(UIButtonTypeRoundedRect)];
    //self.shoucangButton.backgroundColor =[UIColor redColor];
    self.shoucangButton.layer.cornerRadius = 15;
    self.shoucangButton.layer.masksToBounds = YES;
    self.shoucangButton.frame = CGRectMake(15, 15, 30, 30);
         [self.shoucangButton setBackgroundImage:[UIImage imageNamed:@"shoucang"] forState:(UIControlStateNormal)];
    [self.shoucangButton addTarget:self action:@selector(didClickedShouCangButton:) forControlEvents:(UIControlEventTouchUpInside)];
    
     self.shoucangButton.selected = YES;
    [shoucangView addSubview:_shoucangButton];
    
    
    UIView *commentView =[[UIView alloc]initWithFrame:CGRectMake(Screenwidth-30-60, (self.topView.frame.size.height-90)/2-20, 60, 60)];
   // commentView.backgroundColor = [UIColor greenColor];
    [self.topView addSubview:commentView];
    self.commentLabel =[[UILabel alloc]initWithFrame:CGRectMake(0, 45, 60, 15)];
   // self.commentLabel.backgroundColor = [UIColor magentaColor];
    self.commentLabel.textAlignment = 1;
    self.commentLabel.textColor =[UIColor whiteColor];
    [commentView addSubview:_commentLabel];
    
    //评论按钮
    UIButton *commentButton =[UIButton buttonWithType:(UIButtonTypeRoundedRect)];
   // commentButton.backgroundColor =[UIColor redColor];
    commentButton.layer.cornerRadius = 15;
    [commentButton setBackgroundImage:[UIImage imageNamed:@"comment"] forState:(UIControlStateNormal)];
    [commentButton addTarget:self action:@selector(didClickedCommentButton) forControlEvents:(UIControlEventTouchUpInside)];
    commentButton.layer.masksToBounds = YES;
    commentButton.frame = CGRectMake(15, 15, 30, 30);
    
    [commentView addSubview:commentButton];
}
//创建collectionView
-(void)createColllectionView
{
    [self creatView];
    UICollectionViewFlowLayout *mainFlowLayout =[[UICollectionViewFlowLayout alloc]init];
    mainFlowLayout.itemSize = CGSizeMake(Screenwidth, Screenheight-49 -64-(Screenheight/2-64));
    mainFlowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    mainFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    mainFlowLayout.minimumLineSpacing = 0;
    mainFlowLayout.minimumInteritemSpacing = 0;
  // mainFlowLayout.headerReferenceSize=CGSizeMake(Screenwidth, Screenheight/2-64);
    self.mainCollectionView =[[UICollectionView alloc]initWithFrame:CGRectMake(0, Screenheight/2-94+50, Screenwidth, Screenheight-64-49-(Screenheight/2-64)-5) collectionViewLayout:mainFlowLayout];
    self.mainCollectionView.pagingEnabled = YES;
    self.mainCollectionView.delegate =self;
    self.mainCollectionView.dataSource = self;
    self.mainCollectionView.backgroundColor =[UIColor whiteColor];
    [self.view addSubview:_mainCollectionView];

    [self.mainCollectionView registerClass:[YFLCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self.mainCollectionView registerClass:[YFLalbumCollectionViewCell class] forCellWithReuseIdentifier:@"albumCell"];
   
    [self.mainCollectionView registerClass:[ YFLinformationCollectionViewCell class] forCellWithReuseIdentifier:@"informationCell"];
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 4;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 0) {
        NSLog(@"单曲界面");
        YFLCollectionViewCell *cell =[self.mainCollectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        cell.backgroundColor =[UIColor yellowColor];
        // NianFeilongModel *model =[self.sourceArray objectAtIndex:indexPath.item];
        cell.indexBottom = 0;
        cell.dataArray =self.sourceArray;
        [cell.tableView reloadData];
        return cell;
    }if (indexPath.item == 1) {
        NSLog(@"专辑界面");
        YFLalbumCollectionViewCell *cell =[self.mainCollectionView dequeueReusableCellWithReuseIdentifier:@"albumCell" forIndexPath:indexPath];
        //cell.backgroundColor =[UIColor yellowColor];
        cell.dataArray =self.albumArray;
    [cell.littleCollectionView reloadData];
         return cell;
    }if (indexPath.item == 2) {
        NSLog(@"MV界面");
        YFLCollectionViewCell *cell =[self.mainCollectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
        cell.backgroundColor =[UIColor yellowColor];
        cell.indexBottom = 2;
        cell.sourceArray = self.mvArray;
       //  NSLog(@"111111111111111111111111111111%@",cell.sourceArray);
        cell.naVC = self.navigationController;
     //   cell.mvDIC = self.mvUrlDic;
        cell.MVstart = self.MVstartArray;
        
       // NSLog(@"========================tttttttttttt============%@",cell.MVstart);
        [cell.tableView reloadData];
        return cell;
    }if (indexPath.item == 3) {
        NSLog(@"歌手详情");
         YFLinformationCollectionViewCell *cell =[self.mainCollectionView dequeueReusableCellWithReuseIdentifier:@"informationCell" forIndexPath:indexPath];
      //  cell.backgroundColor =[UIColor yellowColor];
        YFLinformationModel *model =[self.informationArray objectAtIndex:0];
        cell.informationDic = self.informationDictionary;
        
        NSLog(@"-------------------%@",cell.informationDic);
        
    cell.Model = model;
        return cell;
    }
    return nil;
    }
//评论按钮点击方法
-(void)didClickedCommentButton
{
    YFLcommentViewController *commentVC =[[ YFLcommentViewController alloc]init];
    commentVC.singerId = self.strID;
    [self.navigationController pushViewController:commentVC animated:YES];
}
//收藏按钮点击方法
-(void)didClickedShouCangButton:(UIButton *)sender
{
    if (sender.selected == YES) {
        [self.shoucangButton setBackgroundImage:[UIImage imageNamed:@"shoucangs"] forState:(UIControlStateNormal)];
        NSLog(@"收藏成功");
        
        self.shoucangButton.selected = NO;
    }else{
         [self.shoucangButton setBackgroundImage:[UIImage imageNamed:@"shoucang"] forState:(UIControlStateNormal)];
        NSLog(@"收藏取消");
         self.shoucangButton.selected = YES;
    }
    NSLog(@"点击收藏");
}

//segMentController点击方法
- (void)switchPage:(UISegmentedControl *)button
{
//    if (button.selectedSegmentIndex == 0) {
//       //  [self.sourceArray removeAllObjects];
//        [self.mainCollectionView reloadData];
//        [self  getNetData];
//        
//    }else if (button.selectedSegmentIndex == 1) {
//        
//        [self getAlbumNetData];
//        
//    }else if (button.selectedSegmentIndex == 2) {
//       // [self.mvArray removeAllObjects];
//        [self.mainCollectionView reloadData];
//
//        [self getMVData];
//    }else if (button.selectedSegmentIndex == 3) {
//          NSLog(@"点击了歌手信息");
//         }

    NSInteger index = button.selectedSegmentIndex;
    [self.mainCollectionView reloadData];
    [self.mainCollectionView setContentOffset:CGPointMake(Screenwidth*index, 0)animated:YES];
}
//关联方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{   NSInteger index = self.mainCollectionView.contentOffset.x / Screenwidth;
  //  NSLog(@"9999999999999999=========%ld",index);
   self.segmentController.selectedSegmentIndex = index;
     [self.mainCollectionView reloadData];
}
//获取评论数据
-(void)getCommentNetData
{
    NSString *commentStr =[NSString stringWithFormat:@"http://api.comment.dongting.com/comment/latest?type=singer&subject_id=%@&utdid=V1Uldacj6bcDADkjpiFXHgHN&page=1&size=10&app=ttpod&v=v9.0.2.2016052421&user_id=0&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=8.1&active=1&net=2&openudid=c9b07ac0973c329512858440f1331ada894f0a70&idfa=788F6F0F-81B5-4A98-A2AC-164A37DAA099&utdid=V1Uldacj6bcDADkjpiFXHgHN&alf=(null)&bundle_id=com.ttpod.music&latitude=38.88339002821181&longtitude=121.5446427408854&access_token=(null)&vc=9000200",self.strID];
    
    
 //   NSLog(@"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%@",commentStr);
    
    [MyTools getNetWithUrl:commentStr body:nil header:nil response:LHQJSON success:^(id result) {
        
        NSArray *array =(NSArray *)result;
        
        for (NSDictionary *dic in array) {
            NSString *str = [dic objectForKey:@"total_storey"];
            self.commentLabel.text = str.description;
        }
     
    } failure:^(NSError *error) {
        
    }];
}
//获取专辑数据
-(void)getAlbumNetData
{
    self.albumArray =[NSMutableArray array];
    NSString *str =[NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@%@",self.strID,@"/albums?page=1&app=ttpod&v=v9.0.2.2016052421&user_id=0&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=8.1&active=1&net=2&openudid=c9b07ac0973c329512858440f1331ada894f0a70&idfa=788F6F0F-81B5-4A98-A2AC-164A37DAA099&utdid=V1Uldacj6bcDADkjpiFXHgHN&alf=(null)&bundle_id=com.ttpod.music&latitude=38.88330485026042&longtitude=121.5446286349826&access_token=(null)&vc=9000200"];
    
    [MyTools getNetWithUrl:str body:nil header:nil response:LHQJSON success:^(id result) {
        NSDictionary *dictionary =(NSDictionary *)result;
        
        NSArray *array =[dictionary objectForKey:@"data"];
        
        for (NSDictionary *dic in array) {
            YFLalbumModel *albumModel =[YFLalbumModel new];
            [albumModel setValuesForKeysWithDictionary:dic];
            
            [self.albumArray addObject:albumModel];
        }
        
        [self.mainCollectionView reloadData];
    } failure:^(NSError *error) {
        
    }];
  
}
//获取MV数据
-(void)getMVData{
    self.mvArray =[NSMutableArray array];
     self.mvUrlDic =[NSDictionary dictionary];
    
    self.MVstartArray =[NSMutableArray array];
 //   NSArray *arrayMV =[NSArray array];
    NSString *str =[NSString stringWithFormat: @"http://api.dongting.com/song/singer/%@%@",self.strID,@"/videos?page=1&app=ttpod&v=v9.0.2.2016052421&user_id=0&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=8.1&active=1&net=2&openudid=c9b07ac0973c329512858440f1331ada894f0a70&idfa=788F6F0F-81B5-4A98-A2AC-164A37DAA099&utdid=V1Uldacj6bcDADkjpiFXHgHN&alf=(null)&bundle_id=com.ttpod.music&latitude=38.88328938802083&longtitude=121.5445806206597&access_token=(null)&vc=9000200"];
   
    NSLog(@"=========================MV================%@",str);
    [MyTools getNetWithUrl:str body:nil header:nil response:LHQJSON success:^(id result) {
    
        NSDictionary *dictionary = (NSDictionary *)result;
        
        NSArray *array =[dictionary objectForKey:@"data"];
        for (NSDictionary *dic in array) {
            
            NSArray *arrayMV =[dic objectForKey:@"mvList"];
            
            NSLog(@"================6666666666666==============%@",arrayMV);
            
            NSArray *arrlinshi =[NSArray arrayWithObject:[arrayMV objectAtIndex:0]];
            
            NSLog(@"----------------------------------- --------------------%@",arrlinshi);
            
            for (NSDictionary *diction in arrlinshi) {
        //        NSLog(@"================777777777777777777777==============%@",diction);
                //self.mvUrlDic  = diction;
                
                [self.MVstartArray addObject:diction];
              //  NSLog(@"================8888888888888888888888==============%ld",self.MVstartArray.count);
            }
            
            
            
            YFLmvModel *MVmodel =[YFLmvModel new];
            
            [MVmodel setValuesForKeysWithDictionary:dic];
            [self.mvArray addObject:MVmodel];
            
         //   NSLog(@"======================%@",self.mvArray);
        }
        [self.mainCollectionView reloadData];
    } failure:^(NSError *error) {
        
    }];
}
//获取数据
-(void)getNetData
{
    self.sourceArray =[NSMutableArray array];
    NSString *str = [NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@%@",self.strID,@"/songs?page=1&size=50&app=ttpod&v=v9.0.2.2016052421&user_id=0&mid=iPhone8%2C1&f=f320&s=s310&imsi=&hid=&splus=9.3.1&active=1&net=2&openudid=a435546e84876b149d2058bd4f519680e815c457&idfa=DB82CD6A-AC09-4AA6-BB95-4D4C536F8E07&utdid=Vu9c0yfFE5kDACwlSAWrz4%2B%2F&alf=(null)&bundle_id=com.ttpod.music&latitude=38.88328450520833&longtitude=121.5445640733507&access_token=(null)&vc=9000200"];
    ;
    
   // NSLog(@"777777777777777777777=================------%@",str);
    
    [MyTools getNetWithUrl:str body:nil header:nil response:LHQJSON success:^(id result) {
        
        
        NSDictionary *dictionary = (NSDictionary *)result;
        
       self.shoucangCount = [dictionary objectForKey:@"totalCount"];
       
        self.shoucangLabel.text =self.shoucangCount.description;
        
        NSArray *array =[dictionary objectForKey:@"data" ];
        
        for (NSDictionary *dic in array) {
            NianFeilongModel *model =[NianFeilongModel new];
            [model setValuesForKeysWithDictionary:dic];
            [self.sourceArray addObject:model];
        }
        [self.mainCollectionView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
 
}
//获取歌手信息
-(void)getInformationData
{
    self.informationArray =[NSMutableArray array];
    self.informationDictionary =[NSDictionary dictionary];
   
    NSString *string =[NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@%@",self.strID,@"?detail=true&app=ttpod&v=v9.0.2.2016052421&user_id=0&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=8.1&active=1&net=2&openudid=c9b07ac0973c329512858440f1331ada894f0a70&idfa=788F6F0F-81B5-4A98-A2AC-164A37DAA099&utdid=V1Uldacj6bcDADkjpiFXHgHN&alf=(null)&bundle_id=com.ttpod.music&latitude=38.88330485026042&longtitude=121.5446286349826&access_token=(null)&vc=9000200"];
    
    NSLog(@"666666666666666666666666%@",string);
    
    [MyTools getNetWithUrl:string body:nil header:nil response:LHQJSON success:^(id result) {
        
        NSDictionary *dictionary = (NSDictionary *)result;
        NSArray *array =[dictionary objectForKey:@"data"];
        
        NSDictionary *dic = (NSDictionary *)array;
        self.informationDictionary = dic;
        YFLinformationModel *informationModel =[YFLinformationModel new];
      
        [informationModel setValuesForKeysWithDictionary:dic];
        
        [self.informationArray addObject:informationModel];
        
        NSLog(@"=======77777777777777777777=====%@",self.informationArray);
       
        
        
        
            
           
           
       
        
        [self.mainCollectionView reloadData];
    } failure:^(NSError *error) {
        
    } ];
    
    
    
    
}
////禁止旋转屏幕
//- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
//
//{
//    
//    return (UIInterfaceOrientationMaskPortrait);
//    
//}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
